package cn.edu.hqu.wel.shiyan1.service;

import cn.edu.hqu.wel.shiyan1.model.Comment;
import cn.edu.hqu.wel.shiyan1.model.Movie;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommentService {

    public List<Comment> getCommentsByMovieId(Long movieId) {
        return Comment.getCommentsByMovieId(movieId);
    }

    public Comment addComment(Comment comment) {
        Comment.addComment(comment);
        return comment;
    }

    public boolean deleteComment(Long id) {
        Optional<Movie> movie = Movie.getMovieById(id);
        if (movie.isPresent()) {
            movie.get().getComments().removeIf(comment -> comment.getId().equals(id));
            return true;
        }
        return false;
    }

    public Comment likeComment(Long id) {
        Optional<Comment> comment = Comment.getCommentById(id);
        if (comment.isPresent()) {
            comment.get().setLikes(comment.get().getLikes() + 1);
            return comment.get();
        }
        return null;
    }
}